package com.ly.blog2Admin.service.impl;

import com.ly.blog2Admin.dao.SysLoginLogMapper;
import com.ly.blog2Admin.dao.model.SysLoginLog;
import com.ly.blog2Admin.service.SysLoginLogService;
import com.ly.blog2Common.core.page.MybatisPageHelper;
import com.ly.blog2Common.core.page.PageRequest;
import com.ly.blog2Common.core.page.PageResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Log4j2
@Service
public class SysLoginLogServiceImpl  implements SysLoginLogService {

	@Autowired
	private SysLoginLogMapper sysLoginLogMapper;

	@Override
	public int save(SysLoginLog record) {
		if(record.getId() == null || record.getId() == 0) {
			return sysLoginLogMapper.insertSelective(record);
		}
		return sysLoginLogMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int delete(SysLoginLog record) {
		return sysLoginLogMapper.deleteByPrimaryKey(record.getId());
	}

	@Override
	public int delete(List<SysLoginLog> records) {
		for(SysLoginLog record:records) {
			delete(record);
		}
		return 1;
	}

	@Override
	public SysLoginLog findById(Long id) {
		return sysLoginLogMapper.selectByPrimaryKey(id);
	}

	@Override
	public PageResult findPage(PageRequest pageRequest) {
		Object userName = pageRequest.getParam("userName");
		if(userName != null) {
			return MybatisPageHelper.findPage(pageRequest, sysLoginLogMapper, "findPageByUserName", userName);
		}
		Object status = pageRequest.getParam("status");
		if(status != null) {
			return MybatisPageHelper.findPage(pageRequest, sysLoginLogMapper, "findPageByStatus", status);
		}
//		return MybatisPageHelper.findPage(pageRequest, sysLoginLogMapper);
//		return MybatisPageHelper.findPage(pageRequest, sysLoginLogMapper);
		return MybatisPageHelper.findPage(pageRequest, sysLoginLogMapper, "findPageByStatus", "");

	}

	@Override
	public void saveLoginLog(String username, String ipAddr) {
		saveLogoutLog(username,ipAddr,SysLoginLog.LOGIN_STATE_STOP);

		SysLoginLog sysLoginLog = new SysLoginLog(username,"online",ipAddr);
		save(sysLoginLog);
	}

	@Override
	public void saveLogoutLog(String username, String ipAddr, String type) {
		List<SysLoginLog> sysLoginLogList = findByUserNameAndIpAddr(username,ipAddr);
		for(SysLoginLog sysLoginLog:sysLoginLogList){
			if(SysLoginLog.LOGIN_STATE_ONLINE.equals(sysLoginLog.getStatus())){
				sysLoginLog.setStatus(
						SysLoginLog.LOGIN_STATE_LOGOUT == type ? SysLoginLog.LOGIN_STATE_LOGOUT :
								(SysLoginLog.LOGIN_STATE_TIMEOUT == type ? SysLoginLog.LOGIN_STATE_TIMEOUT : SysLoginLog.LOGIN_STATE_STOP)
				);
				sysLoginLog.setLastUpdateTime(new Date());
				save(sysLoginLog);
				log.info("类型{}。用户 {} 退出登录，上次时间：{}",type,sysLoginLog.getCreateTime());
			}
		}
	}

	@Override
	public List<SysLoginLog> findByUserNameAndIpAddr(String username, String ipAddr) {
		return sysLoginLogMapper.findByUserNameAndIpAddr(username,ipAddr);
	}
}
